package ua.edu.nuos.tees.jdmmon.gui.dialogs;

import ua.edu.nuos.tees.jdmmon.bl.MultimeterService;
import ua.edu.nuos.tees.jdmmon.dao.implementations.Rs232Dao;
import utils.ConfigUtils;
import vo.Config;
import vo.PATHS;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.util.Properties;

/**
 * Dialog for menu item connection
 *
 * @author Olha Mudrievska
 *         23.05.13
 */
public class ConnectionDialog extends JDialog {

    private JPanel contentPane = new JPanel();
    private JButton buttonConnect;
    private JButton buttonCancel;
    private JLabel portNameLabel;
    private JLabel baudRateLabel;
    private JLabel dataBitsLabel;
    private JLabel stopBitsLabel;
    private JLabel parityLabel;
    private JComboBox portNameComboBox;
    private JComboBox baudRateComboBox;
    private JComboBox dataBitsComboBox;
    private JComboBox stopBitsComboBox;
    private JComboBox parityComboBox;
    private Config config;
    private Rs232Dao dao;

    public ConnectionDialog(final boolean state) {

        setContentPane(contentPane);
        contentPane.setLayout(new GridBagLayout());
        setModal(true);

        portNameLabel = new JLabel("PORT:");
        String portNames[] = Rs232Dao.getComPortNames();
        portNameComboBox = new JComboBox(portNames);

        baudRateLabel = new JLabel("Baud Rate: ");
        String baudRates[] = {"9600"};
        baudRateComboBox = new JComboBox(baudRates);

        dataBitsLabel = new JLabel("Data bits: ");
        String dataBits[] = {"8"};
        dataBitsComboBox = new JComboBox(dataBits);

        stopBitsLabel = new JLabel("Stop bits: ");
        String stopBits[] = {"1"};
        stopBitsComboBox = new JComboBox(stopBits);

        parityLabel = new JLabel("Parity: ");
        String parity[] = {"none"};
        parityComboBox = new JComboBox(parity);

        buttonConnect = new JButton("CONNECT");
        buttonCancel = new JButton("CANCEL");

        addComponent(portNameLabel, contentPane, GridBagConstraints.CENTER, 1, 1, 0.5, 0.5, 0, 0);
        addComponent(portNameComboBox, contentPane, GridBagConstraints.WEST, 1, 1, 0.5, 0.5, 1, 0);
        addComponent(baudRateLabel, contentPane, GridBagConstraints.CENTER, 1, 1, 0.5, 0.5, 0, 1);
        addComponent(baudRateComboBox, contentPane, GridBagConstraints.WEST, 1, 1, 0.5, 0.5, 1, 1);
        addComponent(dataBitsLabel, contentPane, GridBagConstraints.CENTER, 1, 1, 0.5, 0.5, 0, 2);
        addComponent(dataBitsComboBox, contentPane, GridBagConstraints.WEST, 1, 1, 0.5, 0.5, 1, 2);
        addComponent(parityLabel, contentPane, GridBagConstraints.CENTER, 1, 1, 0.5, 0.5, 0, 3);
        addComponent(parityComboBox, contentPane, GridBagConstraints.WEST, 1, 1, 0.5, 0.5, 1, 3);
        addComponent(stopBitsLabel, contentPane, GridBagConstraints.CENTER, 1, 1, 0.5, 0.5, 0, 4);
        addComponent(stopBitsComboBox, contentPane, GridBagConstraints.WEST, 1, 1, 0.5, 0.5, 1, 4);
        addComponent(buttonConnect, contentPane, GridBagConstraints.EAST, 1, 1, 0.5, 1.0, 0, 5);
        addComponent(buttonCancel, contentPane, GridBagConstraints.CENTER, 1, 1, 0.5, 1.0, 1, 5);

        buttonConnect.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    if (state) {
                        dao.setBaudRate(Integer.parseInt((String) baudRateComboBox.getSelectedItem()));
                        dao.setDataBits(Integer.parseInt((String) dataBitsComboBox.getSelectedItem()));
                        dao.setDataBits(Integer.parseInt((String) stopBitsComboBox.getSelectedItem()));
                        dao.setParity(Integer.parseInt((String) parityComboBox.getSelectedItem()));
                    }
                    config = new Config(new Properties(), PATHS.COM_CONFIG);
                    config.appendProperty("portName", (String) portNameComboBox.getSelectedItem());
                    config.appendProperty("baudRate", (String) baudRateComboBox.getSelectedItem());
                    config.appendProperty("dataBits", (String) dataBitsComboBox.getSelectedItem());
                    config.appendProperty("stopBits", (String) stopBitsComboBox.getSelectedItem());
                    config.appendProperty("parity", (String) parityComboBox.getSelectedItem() == "none" ? "0" : "1");
                    ConfigUtils.storeConfig(config);

                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                dispose();
            }
        });

        buttonCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });

        /**
         *  Exit on ESCAPE
         */
        contentPane.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    }


    public void setDao(MultimeterService multimeterService) {
        dao = (Rs232Dao) multimeterService.getDAO();
    }

    /**
     * Add component to container
     *
     * @param anchor     the initial anchor value
     * @param gridwidth  the initial gridwidth value
     * @param gridheight the initial gridheight value
     * @param weightx    the initial weightx value
     * @param weighty    the initial weighty value
     * @param gridx      the initial gridx value
     * @param gridy      the initial gridy value
     */
    private void addComponent(Component component, Container pane, int anchor, int gridwidth, int gridheight,
                              double weightx, double weighty, int gridx, int gridy) {
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.anchor = anchor;
        gridBagConstraints.gridwidth = gridwidth;
        gridBagConstraints.gridheight = gridheight;
        gridBagConstraints.weightx = weightx;
        gridBagConstraints.weighty = weighty;
        gridBagConstraints.gridx = gridx;
        gridBagConstraints.gridy = gridy;
        pane.add(component, gridBagConstraints);
    }
}

